package com.tianji.learning.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.tianji.learning.utils.TableInfoContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MybatisPlusConfig {
    @Bean
    DynamicTableNameInnerInterceptor tableNameInnerInterceptor() {
        Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<>(1);
        //每次持久化历史排行榜是都会被拦截，如果是points_board_表则会被替换     需要将其注册到mybatisPlus的核心拦截器里
        tableNameHandlerMap.put("points_board", (sql, tableName) -> TableInfoContext.getTableName());
        return new DynamicTableNameInnerInterceptor(tableNameHandlerMap);
    }
}
